package com.aaa.qy127.dao.impl;

import com.aaa.qy127.dao.RoleDao;
import com.aaa.qy127.entity.Role;
import com.aaa.qy127.util.JDBCUtil;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component("roleDaoImpl")
public class RoleDaoImpl implements RoleDao {
    /**
     * 根据rid 查询 该 角色 的状态
     * @param rid
     * @return
     */
    @Override
    public int getOneStatusByRid(int rid) {
        String sql = "select `status` from t_role where rid = ?";
        List<List> lists = JDBCUtil.queryList(sql, rid);
        if (lists.size()>0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 3;
    }

    /**
     * 查询全部的 role信息
     * @return
     */
    @Override
    public List<Map<String, Object>> getAllRole() {
        String sql = "select rid,rname,memo,status from t_role";
        List<Map<String, Object>> maps = JDBCUtil.queryMap(sql);
        if (maps.size() > 0){
            return maps;
        }
        return null;
    }

    /**
     * 查询 每页 的 数量
     * @param role
     * @return
     */
    @Override
    public int getCountByLimit(Role role) {
        String sql = "select count(*) from t_role LIMIT ?,?;";
        List<List> lists = JDBCUtil.queryList(sql, (role.getPage() - 1) * role.getLimit(), role.getLimit());
        if (lists.size() > 0){
            Object o = lists.get(0).get(0);
            int i = Integer.parseInt(o + "");
            return i;
        }
        return 0;
    }

    /**
     * 新增一条 role信息
     * @param role
     * @return
     */
    @Override
    public int insertOneRole(Role role) {
        String sql = "insert into t_role values(null,?,?,?)";
        int i = JDBCUtil.update(sql, role.getRname(), role.getMemo(), role.getStatus());
        return i;
    }

    /**
     * 修改一条 role 信息
     * @param role
     * @return
     */
    @Override
    public int updateOneRole(Role role) {
        String sql = "update t_role set rname=?,memo=?,status=? where rid=?";
        int i = JDBCUtil.update(sql, role.getRname(), role.getMemo(), role.getStatus(), role.getRid());
        return i;
    }

    /**
     * 删除一条 role 信息
     * @param rid
     * @return
     */
    @Override
    public int deleteOneRole(String rid) {
        String sql = "delete from t_role where rid = ?";
        int i = JDBCUtil.update(sql, rid);
        return i;
    }
}
